import java.io.BufferedWriter;
import java.io.FileWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ItemWriter {
	public static final Class<ItemDefinition> itemClass = ItemDefinition.class;

	public static void dumpItems() {
		String itemClassName = ItemWriter.itemClass.getName();
		Method[] methods = ItemWriter.itemClass.getMethods();
		Field[] fields = ItemWriter.itemClass.getFields();
		Method getItem = null;
		for (Method method : methods) {
			if (Modifier.isPublic(method.getModifiers())
			&& Modifier.isStatic(method.getModifiers())
			&& (method.getReturnType() == ItemWriter.itemClass)) {
				getItem = method;
				break;
			}
		}
		if (getItem == null) {
			System.err.println("Error: Cannot find getItem method");
			System.err.println("Make sure it is the first type \"public static " + itemClassName
			+ "\" method in " + itemClassName);
			return;
		}
		int itemCount = -1;
		for (Field field : fields) {
			if ((field.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC))
			&& (field.getType() == int.class)) {
				try {
					itemCount = field.getInt(null);
					break;
				} catch (Exception exception) {
				}
			}
		}
		if (itemCount == -1) {
			System.err.println("Error: Cannot find itemCount field");
			System.err.println("Make sure it is the first type \"public static int\" field in "
			+ itemClassName);
			return;
		}
		try {
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
			"ItemConfiguration.txt", false));
			bufferedWriter.write("Items: " + itemCount);
			for (int items = 0; items < 7956; items++) {
				bufferedWriter.newLine();
				Object item = getItem.invoke(null, items);
				bufferedWriter.write("if (i == " + items + ") {");
				for (Field field : fields) {
					if (field.getModifiers() != Modifier.PUBLIC) {
						continue;
					}
					Class fieldType = field.getType();
					String fieldName = field.getName();
					Object fieldValue = field.get(item);
					if (fieldType.isArray()) {
						Object array = field.get(item);
						if (array == null) {
							//bufferedWriter.newLine();
							//bufferedWriter.write(fieldName + ": null");
						} else {
							if (fieldType == byte[].class) {
								StringBuilder stringBuilder = new StringBuilder();
								for (int len = 0; len < Array.getLength(fieldValue); len++) {
									stringBuilder.append((char) Array.getByte(fieldValue, len));
								}
								if (fieldName != "description") {
									bufferedWriter.newLine();
									bufferedWriter.write("itemDefinition." + fieldName + " = "
									+ stringBuilder.toString() + ";");
								} else {
									bufferedWriter.newLine();
									bufferedWriter.write("itemDefinition." + fieldName + " = \""
									+ stringBuilder.toString() + "\".getBytes();");
								}
							} else {
								bufferedWriter.newLine();
								bufferedWriter.write("itemDefinition." + fieldName + " = new int["
								+ Array.getLength(fieldValue) + "];");
								for (int len = 0; len < Array.getLength(fieldValue); len++) {
									if (Array.get(fieldValue, len) != null) {
										if (fieldName == "actions") {
											bufferedWriter.newLine();
											bufferedWriter.write("itemDefinition." + fieldName
											+ "[" + len + "] = \"" + Array.get(fieldValue, len)
											+ "\";");
										} else {
											bufferedWriter.newLine();
											bufferedWriter
											.write("itemDefinition." + fieldName + "[" + len
											+ "] = " + Array.get(fieldValue, len) + ";");
										}
									}
								}
							}
						}
					} else {
						if (fieldName != "id") {
							if (fieldName != "name") {
								bufferedWriter.newLine();
								bufferedWriter.write("itemDefinition." + fieldName + " = "
								+ field.get(item) + ";");
							} else {
								bufferedWriter.newLine();
								bufferedWriter.write("itemDefinition." + fieldName + " = \""
								+ field.get(item) + "\";");
							}
						}
					}
				}
				bufferedWriter.newLine();
				bufferedWriter.write("}");
			}
			bufferedWriter.flush();
			bufferedWriter.close();
		} catch (Exception exception) {
		}
	}
}
